<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Mdsparts */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="mdsparts-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'slug')->textInput(['maxlength' => true]) ?>

    <div class="form-group field-category-title required">
        <label class="control-label" for="category-level">level</label>
        <select id="category-level" class="form-control" name="Category[level]" >
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
        </select>

        <div class="help-block"></div>
    </div>

    <div class="form-group field-category-title required">
        <label class="control-label" for="category-title" >parent</label>
        <select id="category-parent"
                class="form-control form-parent"
                style="margin-bottom:20px; display:none"
                name="Category[parent]" >
        </select>


        <select id="category-parent_sub"
                class="form-control form-parent_sub"
                style="display:none"
                name="Category[parent_sub]" >

        </select>

        <div class="help-block"></div>
    </div>


    <div class="form-group">
        <?= Html::submitButton(
            $model->isNewRecord ? 'Create' : 'Update',
            ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
        )
        ?>
    </div>

    <?php ActiveForm::end(); ?>

    <script>

        function categoryMap( level ){

            $('#category-parent').hide();
            $('#category-parent_sub').hide();


            if( level == 0 ){
                $('#category-parent').hide();
                $('#category-parent_sub').hide();
            }else if( level == 1 ){
                $('#category-parent').show();
            }else{
                $('#category-parent').show();
                $('#category-parent_sub').show();
            }
        }

        function getChildCategory( id, level ){
            var url = '/backend/web/index.php?r=category/child&'
            + 'id=' + id + "&level=" + level;
//            var data = {
//                'id' : $('#category-parent').val()
//            };
            $.get(url, {}, function( json ){

                if( json.status == 1){
                    var cats = json.category;
                    var options = '';
                    var l = cats.length;
                    for( var i= 0; i < l; i ++){
                        options += "<option value=\"" + cats[i].id + "\">"
                            + cats[i].title
                            + "</option>"
                    }

                    if( level == 0 ){
                        $('#category-parent').html( options );
                        getChildCategory( cats[0].id, 1 );
                    }else if( level == 1 ){
                        $('#category-parent_sub').html( options );
                    }
                }

            });
        }

        //初始化根分类
        getChildCategory( 0, 0 );

        //todo : 三级联动
        $('#category-level').change(function(){
            var level = this.value;
            categoryMap( level );

        });
        $('#category-parent').change(function(){
            var id = this.value;
            getChildCategory( id, 1 );
        });
    </script>
</div>
